package org.eclipse.wtp.sample.flashcards;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SetupServlet extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				
		// read data from form
		String setName = request.getParameter("set");
		String userName = request.getParameter("user");
		String mode = request.getParameter("mode");
			
		// reset message
		request.getSession().setAttribute("message", "");
		
		// report activity FOR DEBUGGING
		System.out.println("setName: " + setName);
		System.out.println("mode: " + mode);

		// forward immediately for New Dictionary
		if (mode.equals("New Dictionary")) {
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/newdictionary.jsp");
			dispatcher.forward(request,response);
			return;
		}
		
		// load User
		User user = new User(userName);
		
		// get local FlashCardSet
		FlashCardSet flashcardset = (FlashCardSet)request.getSession().getAttribute("flashcardset");
		
		// load FlashCardSet if necessary
		if ( (flashcardset != null) && (flashcardset.getTitle().equals(setName)) ) {
			// don't need to load a new set
			System.out.println("Didn't need to load a set from file.");
		}
		else {
			// need to load set from file
			System.out.println("LOADING NEW DICTIONARY FROM FILE");
			flashcardset = new FlashCardSet(setName);
			// load data from file
			response.setContentType("text/html");
			String filename = "/data/" + flashcardset.getFilename();
			ServletContext context = getServletContext();
			InputStream inp = context.getResourceAsStream(filename);
			//if (inp != null) {
				InputStreamReader isr = new InputStreamReader(inp);
				BufferedReader reader = new BufferedReader(isr);
				flashcardset.load(reader);
				reader.close();
				isr.close();
			//}
				
			// register Session attributes
			request.getSession().setAttribute("flashcardset", flashcardset);
		}
		
		
		
		// redirect to correct jsp
		if (mode.equals("Start Quiz")) {
			// check for insufficient cards in deck
			FlashCardSet theSet = (FlashCardSet)request.getSession().getAttribute("flashcardset");
			if (theSet.size() < 3) {
				request.getSession().setAttribute("message", "There are not enough cards in this deck to start a quiz. Why don't you add a few?");
				getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);
			}
			else {
				// instantiate test hand
				FlashCardHand flashcardhand = new FlashCardHand(theSet);
				
				// initialize score
				request.getSession().setAttribute("correct", new Integer(0));
				request.getSession().setAttribute("incorrect", new Integer(0));
				request.setAttribute("result", "");
				
				// save FlashCardHand to Session
				request.getSession().setAttribute("flashcardhand", flashcardhand);
				getServletContext().getRequestDispatcher("/test.jsp").forward(request, response);
			}
		}	
		else if (mode.equals("Lookup") ) {
			request.getSession().setAttribute("response1", "");
			request.getSession().setAttribute("response2", "");
			getServletContext().getRequestDispatcher("/lookup.jsp").forward(request, response);
		}
		else if (mode.equals("Edit Dictionary"))  {
			getServletContext().getRequestDispatcher("/edit.jsp").forward(request, response);
		}
		else {
			// redirect back to main page by default
			getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
		}
	}
}